package bean;

import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import model.TransacaoResultado;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;

import sun.java2d.loops.GraphicsPrimitive.TraceReporter;

public class ReadXml {
	private String xml;
	private TransacaoResultado transResult;
	private boolean status;
	
	public ReadXml(String xmlFile){
		
		xml = xmlFile;
		transResult = new TransacaoResultado();
		status = false;	
		
		try {
			this.loadMsgHeader(xml);
			this.loadMsgErro(xml);
			this.loadMsgOk(xml);			
		} catch (Exception e) {
			status = false;
		}
	}		
	
	private void loadMsgHeader(String xmlFile) {
		try {
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new InputSource(new StringReader(xmlFile)));			
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("transacoes");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;					
					transResult.setTipoTransacao(eElement.getElementsByTagName("tipoTransacao").item(0).getTextContent());
					transResult.setCodEmpresa(eElement.getElementsByTagName("codEmpresa").item(0).getTextContent());					
				}
			}								
		} catch (Exception e) {
			//System.out.println("xml - erro 1");
			e.printStackTrace();
		}
	}
	
	private void loadMsgErro(String xmlFile) {
		try {
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new InputSource(new StringReader(xmlFile)));			
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("transacaoResultado");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);
				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;
					
					transResult.setResposta(eElement.getElementsByTagName("resposta").item(0).getTextContent());
					transResult.setMensagem(eElement.getElementsByTagName("mensagem").item(0).getTextContent());					
				}
				status = false;
			}	
		} catch (Exception e) {
			status = true;
			//e.printStackTrace();
		}
	}
	
	private void loadMsgOk(String xmlFile) {
		try {
			
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
			Document doc = dBuilder.parse(new InputSource(new StringReader(xmlFile)));			
			doc.getDocumentElement().normalize();

			NodeList nList = doc.getElementsByTagName("transacaoResultado");
			for (int temp = 0; temp < nList.getLength(); temp++) {
				Node nNode = nList.item(temp);

				if (nNode.getNodeType() == Node.ELEMENT_NODE) {
					Element eElement = (Element) nNode;					
					int tamanho = 0;					
					tamanho = Integer.parseInt(eElement.getElementsByTagName("qtdLinha").item(0).getTextContent());
					transResult.setQtLinhas(tamanho);
					
					System.out.println(String.format("li%03d",tamanho));
					for (int i = 0;i < tamanho;i++){
						transResult.addLinha(eElement.getElementsByTagName(String.format("li%03d",i+1)).item(0).getTextContent());																		
					}
				}
				status = true;
			}
		} catch (Exception e) {
			status = false;
			//e.printStackTrace();
		}
	}

	public String getXml() {
		return xml;
	}

	public void setXml(String xml) {
		this.xml = xml;
	}

	public TransacaoResultado getTransResult() {
		return transResult;
	}

	public void setTransResult(TransacaoResultado transResult) {
		this.transResult = transResult;
	}

	public boolean isStatus() {
		return status;
	}

	public void setStatus(boolean status) {
		this.status = status;
	}
	
	
	
}
